/**
 * 
 * @param {type} msg
 * @param {type} options
 * @returns {Boolean}
 * 
 * so sanh game
 * thay doi game display name
 * quan ly tab
 * 
 */
function showAlert(msg, options) {
    if (!msg)
        return false;
    var modal = $('#pageAlertModal'),
        onOk = options.onOk ? options.onOk : null;
    if (modal.length === 0) {
        var html = '<div id="pageAlertModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="pageAlertModal" aria-hidden="true">\
                        <div class="modal-dialog">\
                            <div class="modal-content">\
                                <div class="modal-header">\
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\
                                    <h3 id="confirmLable">Thông báo</h3>\
                                </div>\
                                <div class="modal-body"></div>\
                                <div class="modal-footer">\
                                    <a id="btnAlert" class="button sButton bMuddy" href="#" data-dismiss="modal" aria-hidden="true">Đóng</a>\
                                </div>\
                             </div>\
                         </div>\
                    </div>';
        $('body').append(html);
    }
    $('#pageAlertModal').find('.modal-body').html('<p>' + msg + '</p>');
    $('#pageAlertModal').modal();
    $('#pageAlertModal #btnAlert').click(function(param) {
        if ($.isFunction(onOk)) {
            onOk(param);
        } else {
            hideConfirm();
        }
    });
    $('#pageAlertModal').on('hidden.bs.modal', function(param) {
        $('#pageAlertModal').remove();
    });
    return false;
}

function hideAlert() {
    $('#pageAlertModal').modal('hide');
}

function showConfirm(options) {
    var title = options.title ? options.title : 'Thông báo',
        msg = options.msg ? options.msg : '',
        noBt = options.noBt ? options.noBt : 'Đóng',
        yesBt = options.yesBt ? options.yesBt : 'Đồng ý',
        warning = options.warning ? '<p class="text-warning" id="pageConfirmModalWranning" align="left">' + options.warning + '</p>' : '',
        onYes = options.onYes ? options.onYes : null,
        onNo = options.onNo ? options.onNo : null,
        param = options.param ? options.param : null,
        afterShow = options.afterShow ? options.afterShow : null;

    var modal = $('#pageConfirmModal');
    if (modal.length === 0) {
        var html = '<div id="pageConfirmModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirmLable" aria-hidden="true">\
                        <div class="modal-dialog">\
                           <div class="modal-content">\
                               <div class="modal-header">\
                                   <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\
                                   <h3 id="confirmLable">' + title + '</h3>\
                               </div>\
                               <div class="modal-body">' + msg + '</div>\
                               <div class="modal-footer">\
                                   ' + warning + '\
                                   <a class="btn btn-mini" href="#" data-dismiss="modal" aria-hidden="true">' + noBt + '</a>\
                                   <a id="confirmOnYes" class="btn btn-mini btn-danger deleteUser" href="#">' + yesBt + '</a>\
                               </div>\
                            </div>\
                        </div>\
                    </div>';
        $('body').append(html);
        $.isFunction(afterShow) && afterShow(param);
    }
    $('#pageConfirmModal').modal('show');
    $('#pageConfirmModal #confirmOnYes').click(function() {
        if ($.isFunction(onYes)) {
            onYes(param);
        } else {
            hideConfirm();
        }
    });
    $('#pageConfirmModal').on('hidden.bs.modal', function() {
        $.isFunction(onNo) && onNo(param);
        $('#pageConfirmModal').remove();
    });
}

function hideConfirm() {
    $('#pageConfirmModal').modal('hide');
}

function pad(str, max) {
    str = str + '';
    return str.length < max ? pad("0" + str, max) : str;
}

function numberWithCommas(x) {
    return !x || x.length < 3 ? x : x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function validateEmail(x) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(x);
}

function getParamByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function initDatePicker(selector) {
    $(selector).datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        todayHighlight: true,
        language: "vi"
    });
}